<%--
  Created by IntelliJ IDEA.
  User: 59913
  Date: 2021/11/24
  Time: 17:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
    <link href="/static/hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link rel="stylesheet" href="/static/hplus/css/plugins/bootstrap-table/bootstrap-table.min.css">
    <link rel="stylesheet" href="/static/hplus/css/plugins/sweetalert/sweetalert.css">
    <script type="text/javascript" src="/static/hplus/js/jquery.min.js"></script>
    <script src="/static/hplus/js/bootstrap.min.js?v=3.3.6"></script>
    <script type="text/javascript" src="/static/hplus/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="/static/hplus/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="/static/hplus/js/plugins/sweetalert/sweetalert.min.js"></script>
    <!--树型菜单-->
    <link href="${pageContext.request.contextPath}/static/treeview/bootstrap-treeview.min.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/static/treeview/bootstrap-treeview.min.js"></script>
    <script>
        $(function () {
           $("#roletb").bootstrapTable({
               url:"/queryRole",
               height:360,
               pagination:true,
               sidePagination:"server",
               pageSize:5,
               pageList:[5,10],
               columns:[
                   {
                       checkbox:true
                   },
                   {
                       title:"编号",
                       field:"id"
                   },
                   {
                       title:"名称",
                       field:"rname"
                   },
                   {
                       title:"操作",
                       formatter:function (value,row,index) {
                           return "<button class=\"btn btn-primary\" onclick=\"tofenpei("+row.id+")\">分配角色</button> <button class=\"btn btn-primary\" onclick=\"toupd("+row.id+")\">修改</button>\n";
                       },
                       width:200
                   }
               ]
           });
        });
        let rid = 0;
        function tofenpei(roleId) {
            rid=roleId;
            $.post("/toRolePermission",{roleId},function (data) {
                $("#searchTree").treeview({
                    showCheckbox: true,
                    data: data,
                    onNodeChecked: nodeChecked,
                    onNodeUnchecked: nodeUnchecked
                }) ;
            });
            $("#fenpei").modal("show");
        }
        function fenpei() {
            let nodes = $("#searchTree").treeview("getChecked");
            let ids = "";
            nodes.forEach(function (node) {
               ids += node.id+"-";
            });
            $.post("/doRolePermission",{"roleId":rid,ids});
            $("#fenpei").modal("hide");
        }
    </script>
</head>
<body class="container-fluid">
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">角色列表</h3>
    </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-6">
                <button onclick="toadd()" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;增加</button>
            </div>
            <div class="col-sm-6">
                <form  class="form-inline pull-right">
                    <input id="username" type="text" class="form-control">
                    <button class="btn btn-primary" onclick="search()" type="button"><span class="glyphicon glyphicon-search"></span>&nbsp;&nbsp;搜索</button>
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <table id="roletb"></table>
            </div>
        </div>

    </div>
</div>

<!--分配权限的模态框-->
<div class="modal fade" id="fenpei">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">提示信息</h4>
            </div>
            <div class="modal-body">
                <div id="searchTree"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button onclick="fenpei()" type="button" class="btn btn-primary">分配</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
    var treeData = [{
        text: "Parent 1",
        nodes: [{
            text: "Child 1",
            nodes: [{
                text: "Grandchild 1"
            }, {
                text: "Grandchild 2",
                nodes: [{
                    text: "Grandchild 2-1",
                    nodes: [{
                        text: "Grandchild 2-1-1"
                    }, {
                        text: "Grandchild 2-2-1",
                    }]
                }, {
                    text: "Grandchild 1-2",
                }]
            }]
        }, {
            text: "Child 2",
            nodes: [{
                text: "Grandchild 2-1"
            }, {
                text: "Grandchild 2-2",
            }]
        }]
    }, {
        text: "Parent 2",
        id:'11111'
    }, {
        text: "Parent 3"
    }, {
        text: "Parent 4"
    }, {
        text: "Parent 5"
    }];

    var nodeCheckedSilent = false;

    function nodeChecked(event, node) {
        if (nodeCheckedSilent) {
            return;
        }
        nodeCheckedSilent = true;
        checkAllParent(node);
        checkAllSon(node);
        nodeCheckedSilent = false;
    }

    var nodeUncheckedSilent = false;

    function nodeUnchecked(event, node) {
        if (nodeUncheckedSilent)
            return;
        nodeUncheckedSilent = true;
        uncheckAllParent(node);
        uncheckAllSon(node);
        nodeUncheckedSilent = false;
    }

    //选中全部父节点
    function checkAllParent(node) {
        $('#searchTree').treeview('checkNode', node.nodeId, {
            silent: true
        });
        var parentNode = $('#searchTree').treeview('getParent', node.nodeId);
        if (!("nodeId" in parentNode)) {
            return;
        } else {
            checkAllParent(parentNode);
        }
    }
    //取消全部父节点
    function uncheckAllParent(node) {
        $('#searchTree').treeview('uncheckNode', node.nodeId, {
            silent: true
        });
        var siblings = $('#searchTree').treeview('getSiblings', node.nodeId);
        var parentNode = $('#searchTree').treeview('getParent', node.nodeId);
        if (!("nodeId" in parentNode)) {
            return;
        }
        var isAllUnchecked = true; //是否全部没选中
        for (var i in siblings) {
            if (siblings[i].state.checked) {
                isAllUnchecked = false;
                break;
            }
        }
        if (isAllUnchecked) {
            uncheckAllParent(parentNode);
        }

    }

    //级联选中所有子节点
    function checkAllSon(node) {
        $('#searchTree').treeview('checkNode', node.nodeId, {
            silent: true
        });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                checkAllSon(node.nodes[i]);
            }
        }
    }
    //级联取消所有子节点
    function uncheckAllSon(node) {
        $('#searchTree').treeview('uncheckNode', node.nodeId, {
            silent: true
        });
        if (node.nodes != null && node.nodes.length > 0) {
            for (var i in node.nodes) {
                uncheckAllSon(node.nodes[i]);
            }
        }
    }
</script>
</body>
</html>
